package com.ai.mapper.alarm;


import com.ai.entity.alarm.*;

import com.ai.model.made.CameraStatisticsDto;
import com.ai.model.made.RegionalViolationDto;
import com.ai.model.made.TopFiveViolationsDto;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author Mr.Chen
 * @since 2020-06-19
 */
@Repository
public interface RecordAlarmMapper extends BaseMapper<RecordAlarmPO> {

    @Select("SELECT\n" +
            "id id,\n" +
            "camera_id cameraId,\n" +
            "function_type functionType,\n" +
            "snap_photo snapPhoto,\n" +
            "snap_video snapVideo,\n" +
            "is_error signError,\n" +
            "create_time createTime,\n" +
            "is_state isState,\n" +
            "is_error isError,\n" +
            "strip_no stripNo,\n" +
            "original_spray_no originalSprayNo,\n" +
            "detection_spray_no detectionSprayNo,\n" +
            "offset," +
            "detection_times," +
            "strip_width stripWidth," +
            "subgroup subgroup," +
            "is_valid isValid," +
            "duration duration," +
            "remark remark,\n" +
            "review_comments\n" +
            "FROM\n" +
            "record_alarm \n" +
            " ${ew.customSqlSegment} ")
    IPage<RecordAlarmVO> getRecordListQuickly(IPage<RecordAlarmPO> page, @Param("ew") QueryWrapper<RecordAlarmPO> queryWrapper);


    @Select("SELEcT " +
            "  c.date_time as 'name', " +
            "  count( D.id ) as 'value'," +
            "  count( D.id ) as 'max' " +
            "FROM " +
            "  ( " +
            "  SELEcT " +
            "    cASE WHEN " +
            "      @rownum1 IS NULL OR @rownum1 > 5  " +
            "    THEN " +
            "        @rownum1 := - 1  " +
            "    ELSE @rownum1 := @rownum1  " +
            "    END AS col1, " +
            "    @rownum1 := @rownum1 + 1 AS col2, " +
            "    SUBDATE( cURDATE(), DATE_FORMAT( cURDATE(), '%w' )- 1 ) + @rownum1 AS date_time  " +
            "    FROM " +
            "      information_schema.FILES  " +
            "    LIMIT 7  " +
            "    ) c " +
            "LEFT JOIN  " +
            "  ( SELEcT * FROM record_alarm where function_type = #{type}" +
            "  ) D  " +
            "ON c.date_time = DATE_FORMAT( D.create_time, '%Y%m%d' )  " +
            "GROUP BY " +
            "  c.date_time  " +
            "ORDER BY " +
            "  c.date_time")
    List<GraphVO> selectWeekByTye(int type);

    @Select("SELECT  " +
            "  C.week_cn as 'name',  " +
            "  count( D.id ) as 'value',  " +
            "  count( D.id ) as 'max'  " +
            "FROM  " +
            "  (  " +
            "  SELECT DISTINCT  " +
            "    WEEKOFYEAR(  " +
            "      LAST_DAY(  " +
            "      CURDATE())) - WEEKOFYEAR( date_time ) + 1 AS week_cn,  " +
            "    WEEKOFYEAR( date_time ) AS week_y   " +
            "  FROM  " +
            "    (  " +
            "    SELECT  " +
            "    CASE  " +
            "      WHEN  " +
            "        @rownum1 IS NULL   " +
            "        OR @rownum1 > 30   " +
            "      THEN  " +
            "          @rownum1 := - 1 ELSE @rownum1 := @rownum1   " +
            "      END AS col1,  " +
            "      @rownum1 := @rownum1 + 1 AS col2,  " +
            "      DATE_ADD( DATE_ADD( CURDATE(), INTERVAL - DAY ( CURDATE())+ 1 DAY ), INTERVAL @rownum1 DAY ) AS date_time   " +
            "      FROM  " +
            "        information_schema.FILES   " +
            "        LIMIT 32   " +
            "      ) a   " +
            "    WHERE  " +
            "      date_time BETWEEN DATE_ADD( CURDATE(), INTERVAL - DAY ( CURDATE())+ 1 DAY )   " +
            "      AND LAST_DAY(  " +
            "      CURDATE())) C  " +
            "    LEFT JOIN " +
            "       ( SELECT * FROM record_alarm  where function_type = #{type}" +
            "       ) D ON WEEKOFYEAR( D.create_time ) = C.week_y  " +
            "      " +
            "GROUP BY  " +
            "  C.week_cn   " +
            "ORDER BY  " +
            "  C.week_cn #order by col2")
    List<GraphVO> selectMonthByType(int type);


    @Select("SELECT  " +
            "  date_time as 'name',  " +
            "  COUNT( D.id ) as 'value'," +
            "  COUNT( D.id ) as 'max'  " +
            "FROM  " +
            "  (  " +
            "  SELECT  " +
            "    CASE WHEN  " +
            "      @rownum1 IS NULL   " +
            "      OR @rownum1 > 11   " +
            "    THEN  " +
            "        @rownum1 := 0 ELSE @rownum1 := @rownum1   " +
            "    END AS col1,  " +
            "    @rownum1 := @rownum1 + 1 AS date_time   " +
            "  FROM  " +
            "    information_schema.FILES   " +
            "  LIMIT 12   " +
            "    ) C  " +
            "LEFT JOIN " +
            "( SELECT * FROM record_alarm where function_type = #{type} " +
            ") D ON MONTH ( D.create_time ) = C.date_time " +
            "GROUP BY  " +
            "  C.date_time   " +
            "ORDER BY  " +
            "  C.date_time")
    List<GraphVO> selectYearByType(int type);

    @Select("<script>" +
            "SELECT is_state as 'name', " +
            " count(1) as 'value' " +
            "FROM " +
            " record_alarm  " +
            "WHERE " +
            " to_days ( create_time ) = to_days (now())  " +
            " and function_type = #{recordType} " +
            " <if test='cameraId != 0 '>and camera_id = #{cameraId} </if> " +
            "GROUP BY is_state " +
            "</script>")
    List<GraphVO> selectTodayPie(GraphVO graphVO);

    @Select("<script>" +
            "SELECT is_state as 'name', " +
            " count(1) as 'value' " +
            "FROM " +
            " record_alarm  " +
            "WHERE " +
            " create_time > subdate( curdate(), date_format( curdate(), '%w' )- 1 )  " +
            " and function_type = #{recordType} " +
            " <if test='cameraId != 0 '>and camera_id = #{cameraId} </if> " +
            "GROUP BY is_state " +
            "</script>")
    List<GraphVO> selectFewDaysPie(GraphVO graphVO);

    @Select("<script>" +
            "SELECT is_state as 'name', " +
            " count(1) as 'value' " +
            "FROM " +
            " record_alarm  " +
            "WHERE " +
            " create_time > DATE_ADD( curdate(), INTERVAL - DAY ( curdate())+ 1 DAY ) " +
            " and function_type = #{recordType} " +
            " <if test='cameraId != 0 '>and camera_id = #{cameraId} </if> " +
            "GROUP BY is_state " +
            "</script>")
    List<GraphVO> selectLastMonthPie(GraphVO graphVO);

    @Select("<script>" +
            "SELECT create_time as 'name', " +
            " `offset` as 'value' " +
            "FROM " +
            " record_alarm  " +
            "WHERE 1=1 " +
            "  <if test='dateBegin != null'>and create_time &gt;= #{dateBegin} </if> " +
            " <if test='dateEnd != null'>and create_time &lt; #{dateEnd} </if> " +
            " and function_type = #{functionType} " +
            "</script>")
    List<GraphVO> getLineChartByType(RecordSearchEntity record);

    List<RecordAlarmPO> getRecordByCameraCodeAndFunType(@Param("cameraCode") ArrayList<String> cameraCode, @Param("funType") String funType);

    List<CameraStatisticsDto> cameraStatistics();

    List<TopFiveViolationsDto> topFiveViolations();


    List<RegionalViolationDto> regionalViolation();


    String getServerIpByRecord(@Param("recordId") String recordId);


    IPage<RecordAlarmVO> selectRecodAlarm(@Param("page") RecordAlarmPage page, @Param("ew") QueryWrapper<RecordAlarmVO> queryWrapper, @Param("functionType") String functionType);

    @Select("select id from record_alarm where strip_no=#{stripNo} and function_type=#{endDefectDetection}")
    Integer selectIdByStripNo(String stripNo, String endDefectDetection);

    IPage<RecordAlarmVO> selectRecodAlarmTower(RecordAlarmPage page,@Param("ew") QueryWrapper<RecordAlarmVO> queryWrapper);

    /**
     * 获取前五条塔型告警信息
     * @return
     */
    List<RecordAlarmPO> getUpToDateTXAlarm();

}

